# -*- coding: utf-8 -*-
"""
Created on Sat May 19 09:30:02 2018

@author: zy
"""
from RBM import RBM,read_data
import time
from django.http import JsonResponse
'''
受限的玻尔兹曼机:https://blog.csdn.net/zc02051126/article/details/9668439
'''
import matplotlib.pylab as plt
import numpy as np
def rbm_view(request):
    data = read_data('./liearmodel/RBM/text_data.txt')
    data = np.array(data)
    print(data.shape)  # (5000, 784)

    # 创建RBM网络
    rbm = RBM(784, 100, max_epoch=50, learning_rate=0.05)
    # 开始训练
    rbm.fit(data)

    # 显示64张手写数字
    images = data[0:64]
    print(images.shape)
    a = rbm.visualize(images)

    fig = plt.figure(figsize=(12, 4))
    plt.subplot(1, 3, 1)
    plt.imshow(a, cmap=plt.cm.gray)
    plt.title('original data')

    # 显示重构的图像
    rebuild_value = rbm.predict(images)
    b = rbm.visualize(rebuild_value)
    plt.subplot(1, 3, 2)
    plt.imshow(b, cmap=plt.cm.gray)
    plt.title('rebuild data')

    # 显示权重
    w_value = rbm.weight
    c = rbm.visualize(w_value)
    plt.subplot(1, 3, 3)
    plt.imshow(c, cmap=plt.cm.gray)
    plt.title('weight value(w)')
    fig.tight_layout()
    timestamp = str(time.time())
    plt.savefig("./static/" + timestamp + ".png")
    plt.clf()
    return JsonResponse({"code": 200, "message": "success", "data": timestamp + ".png"}, safe=False)